package constant.milk.toilet;

import constant.milk.toilet.database.DbAdapter;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class IntroActivity extends Activity
{
	private Handler mHandler = new Handler() 
	{
		public void handleMessage(Message msg) 
		{
			DbAdapter db = DbAdapter.getInstance();
			db.createTable(IntroActivity.this);
			
			SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
			int firstStartFlag = p.getInt(getString(R.string.prefFirstStart), 0);
			int passwordFlag = p.getInt(getString(R.string.prefPassword), 0);
			
			if (firstStartFlag == 0)
			{
				startFirstActivity();
			}
			else if (passwordFlag > 0)
			{
				startPasswordActivity();
			}
			else
			{
				startMainActivity();
			}
		}; 
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.intro_activity);
		
		mHandler.sendEmptyMessageDelayed(0, 1000);
	}
	
	private void startFirstActivity()
	{
		Intent intent = new Intent(IntroActivity.this, FirstActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
		finish();
	}
	
	private void startPasswordActivity()
	{
		Intent intent = new Intent(IntroActivity.this, PasswordActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
		finish();
	}
	
	private void startMainActivity()
	{
		Intent intent = new Intent(IntroActivity.this, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
		finish();
	}
}
